from PyQt5.QtGui import QFont, QPixmap
from PyQt5.QtCore import pyqtSignal, Qt, QDir
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QGridLayout, QApplication, \
    QLabel, QLineEdit, QPushButton, QSizePolicy, QSpacerItem, QMessageBox
from qdarkstyle import load_stylesheet_pyqt5
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
from Menu import Set_Menu_MainWindow
from U_Widget_Register import Register_Widget
from Dialog_XiuGai_MiMa import XiuGai_MiMa_Dialog

class Login_Widget(QWidget):
    Login_WIdget = pyqtSignal()
    def __init__(self, parent=None):
        super(Login_Widget, self).__init__(parent)
        self.setFixedSize(400, 300)
        self.setWindowTitle("天空偏振光分布模式仿真与探测系统")
        self.currentPath = QDir.currentPath()
        self.currentPath = self.currentPath.replace('/', '\\')
        self.Iconpath = self.currentPath + '\\Icon'
        self.setup_ui()

    def setup_ui(self):
        self.GridLayout = QGridLayout()
        self.GridLayout.setAlignment(Qt.AlignCenter)
        self.BiaoTi_Label = QLabel('天空偏振光分布模式仿真与探测系统')
        self.BiaoTi_Label.setMinimumHeight(20)
        font = QFont()
        font.setPixelSize(23)
        self.BiaoTi_Label.setFont(font)

        self.ZhangHao_Label = QLabel()
        self.ZhangHao_Label.setFixedHeight(32)
        self.ZhangHao_Label.setFixedWidth(32)
        IMG1 = QPixmap(self.Iconpath + "\ZhangHao.png")
        H1 = self.ZhangHao_Label.height()
        W1 = self.ZhangHao_Label.width()
        self.ZhangHao_Label.setPixmap(IMG1)
        self.ZhangHao_Label.setScaledContents(True)

        font.setPixelSize(12)
        self.MiMa_Label = QLabel('密码：')
        self.MiMa_Label.setFixedHeight(32)
        self.MiMa_Label.setFixedWidth(32)
        IMG2 = QPixmap(self.Iconpath + "\密 码.png")
        self.MiMa_Label.setPixmap(IMG2)
        self.MiMa_Label.setScaledContents(True)

        self.ZhangHao_LineEdit = QLineEdit()
        self.ZhangHao_LineEdit.setStyleSheet("""
                            QLineEdit {
                                    background-color:rgb(49, 54, 59);
                                    border-radius:3px;
                                    border: 2px solid rgb(118, 121, 124);
                                    font-family:Times New Roman;
                                    font-size:10pt;
                                    }

                            QLineEdit:hover{
                                        background-color:#426a6a;
                                        color:white;
                                        border: 2px solid rgb(61, 174, 233);
                                        }
                        """)
        self.ZhangHao_LineEdit.setFixedWidth(180)
        self.ZhangHao_LineEdit.setFixedHeight(42)
        self.ZhangHao_LineEdit.setPlaceholderText("请输入用户名")
        self.ZhangHao_LineEdit.setMaxLength(10)

        self.MiMa_LineEdit = QLineEdit()
        self.MiMa_LineEdit.setFixedWidth(180)
        self.MiMa_LineEdit.setFixedHeight(42)
        self.MiMa_LineEdit.setPlaceholderText("请输入密码")
        self.MiMa_LineEdit.setMaxLength(16)
        self.MiMa_LineEdit.setStyleSheet("""
                                    QLineEdit {
                                            background-color:rgb(49, 54, 59);
                                            border-radius:3px;
                                            border: 2px solid rgb(118, 121, 124);
                                            font-family:Times New Roman;
                                            font-size:10pt;
                                            }

                                    QLineEdit:hover{
                                                background-color:#426a6a;
                                                color:white;
                                                border: 2px solid rgb(61, 174, 233);
                                                }
                                """)
        self.MiMa_LineEdit.setEchoMode(QLineEdit.Password)

        font.setPixelSize(18)
        self.Regist_Button = QPushButton('注 册')
        self.Regist_Button.setFixedWidth(90)
        self.Regist_Button.setFixedHeight(40)
        self.Regist_Button.setStyleSheet("""
                                                 QPushButton {font-size: 15pt;
                                                    color: #BDC8E2;
                                                    background-color: #31363b;
                                                    border: 2px solid rgb(118, 121, 124);
                                                    border-radius: 5px;} 

                                                QPushButton:hover {	/* 鼠标悬浮在QPushButton上时的状态 */
                                                    background-color: rgb(66, 106, 106);
                                                    border-color:rgb(61, 174, 233);
                                                    }
                                                QPushButton:pressed { /* 鼠标按压在QPushButton上时的状态 */
                                        	        background-color: #35071f;
                                        	        border-color:rgb(61, 174, 233);}
                                                """)

        self.DengLu_Button = QPushButton('登 录')
        self.DengLu_Button.setFixedWidth(90)
        self.DengLu_Button.setFixedHeight(40)

        self.DengLu_Button.setStyleSheet("""
                                         QPushButton {font-size: 15pt;
                                            color: #BDC8E2;
                                            background-color: #31363b;
                                            border: 2px solid rgb(118, 121, 124);
                                            border-radius: 5px;} 

                                        QPushButton:hover {	/* 鼠标悬浮在QPushButton上时的状态 */
                                            background-color: rgb(66, 106, 106);
                                            border-color:rgb(61, 174, 233);
                                            }
                                        QPushButton:pressed { /* 鼠标按压在QPushButton上时的状态 */
                                	        background-color: #35071f;
                                	        border-color:rgb(61, 174, 233);}
                                        """)
        self.black = QLabel()
        self.black.setFixedHeight(20)

        self.GridLayout.addWidget(self.BiaoTi_Label, 0, 0, 1, 5)
        self.BiaoTi_Label.setAlignment(Qt.AlignCenter)

        self.GridLayout.addWidget(self.BiaoTi_Label, 1, 0, 1, 5)

        self.GridLayout.addWidget(self.ZhangHao_Label, 2, 1, 1, 1)
        self.ZhangHao_Label.setAlignment(Qt.AlignRight)

        self.GridLayout.addWidget(self.ZhangHao_LineEdit, 2, 2, 1, 2)
        self.ZhangHao_LineEdit.setAlignment(Qt.AlignLeft)

        self.GridLayout.addWidget(self.MiMa_Label, 3, 1, 1, 1)
        self.MiMa_Label.setAlignment(Qt.AlignRight)

        self.GridLayout.addWidget(self.MiMa_LineEdit, 3, 2, 1, 2)
        self.MiMa_LineEdit.setAlignment(Qt.AlignLeft)

        self.Below_Layout = QHBoxLayout()

        self.Below_Layout.addWidget(self.Regist_Button)
        self.Below_Layout.addWidget(self.DengLu_Button)

        self.GridLayout.addLayout(self.Below_Layout, 4, 1, 1, 3)


        self.ForgetPassword = QPushButton("忘记密码")
        self.ForgetPassword.setFixedWidth(60)
        self.ForgetPassword.setStyleSheet("""
                                             QPushButton {font-size: 9pt;
                                                color: white;
                                                background-color: #31363b;
                                                border: 2px solid rgb(49, 54, 59);
                                                border-radius: 5px;} 

                                                """)

        self.ChangePassword = QPushButton("修改密码")
        self.ChangePassword.setFixedWidth(60)
        self.ChangePassword.setStyleSheet("""
                                             QPushButton {font-size: 9pt;
                                                color: white;
                                                background-color: #31363b;
                                                border: 2px solid rgb(49, 54, 59);
                                                border-radius: 5px;} 

                                                        """)

        self.belowlayout = QHBoxLayout()
        self.belowlayout.addWidget(self.ForgetPassword)
        self.BelowRight_Spacer = QSpacerItem(250, 5, QSizePolicy.Fixed, QSizePolicy.Fixed)
        self.belowlayout.addSpacerItem(self.BelowRight_Spacer)
        self.belowlayout.addWidget(self.ChangePassword)

        self.GridLayout.addLayout(self.belowlayout, 5, 0, 1, 5)

        self.setLayout(self.GridLayout)

        self.DengLu_Button.clicked.connect(self.DengLu_Button_clicked)
        self.ZhangHao_LineEdit.returnPressed.connect(self.DengLu_Button_clicked)
        self.MiMa_LineEdit.returnPressed.connect(self.DengLu_Button_clicked)

        self.Regist_Button.clicked.connect(self.Regist_Button_clicked)

        self.ChangePassword.clicked.connect(self.Change_PassWord_clicked)
        self.ForgetPassword.clicked.connect(self.Forget_PassWord_clicked)

    def DengLu_Button_clicked(self):
        zhanghao = self.ZhangHao_LineEdit.text()
        mima = self.MiMa_LineEdit.text()

        if zhanghao == '' :
            print(QMessageBox.information(self, "提示", "请输入帐号！", QMessageBox.Yes, QMessageBox.Yes))

        else :
            ShuJuKu = QSqlDatabase.addDatabase('QSQLITE')
            ShuJuKu.setDatabaseName('./db/User_Data.db')
            ShuJuKu.open()
            query = QSqlQuery()
            sql_1 = "SELECT * FROM User WHERE ZhangHao='%s'" % zhanghao
            query.exec_(sql_1)

            if (mima == ''):
                print(QMessageBox.information(self, "提示", "请输入密码！", QMessageBox.Yes, QMessageBox.Yes))
                return
            if (not query.next()):
                print(QMessageBox.warning(self, '警告', '帐号不存在！', QMessageBox.Yes, QMessageBox.Yes))
                return
            if (query.value(3) == 0 and query.value(4) == '否'):
                print(QMessageBox.information(self, "提示", "请联系管理员进行授权!", QMessageBox.Yes, QMessageBox.Yes))
            else:
                if (zhanghao == query.value(0) and mima == query.value(2)):
                    if (query.value(4) == "是"):
                        ShuJuKu.close()
                        self.close()
                        self.Set_Menu_MainWindow = Set_Menu_MainWindow()
                        self.Set_Menu_MainWindow.show()

                    else:
                        print(QMessageBox.information(self, "提示", "请联系管理员进行授权!", QMessageBox.Yes, QMessageBox.Yes))
                    return
                else:
                    QMessageBox.warning(self, '提示', '密码错误！', QMessageBox.Yes, QMessageBox.Yes)
            return

    def Regist_Button_clicked(self):
        self.regist_Widget = Register_Widget()
        self.regist_Widget.show()

    def Change_PassWord_clicked(self):
        self.widget = XiuGai_MiMa_Dialog()
        self.widget.show()

    def Forget_PassWord_clicked(self):
        print(QMessageBox.warning(self, '警告', '请联系管理员进行查看！', QMessageBox.Yes, QMessageBox.Yes))


if __name__ == '__main__':
    from sys import  argv, exit
    app = QApplication(argv)
    app.setStyleSheet(load_stylesheet_pyqt5())
    mainMindow = Login_Widget()
    mainMindow.show()
    exit(app.exec_())
